---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}


```{r setup, include=FALSE, message=FALSE}

# Package setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
pacman::p_load(yaml, arrow, readr, readxl, tidyr, glue, argparse, here, purrr, stringr, dplyr, knitr, kableExtra, janitor);

# Working directory and command line argument setup
current_script = "generate_tables_and_figures/gen_alternate_spec_outputs/src/gen_alternate_spec_outputs.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir = here(dirname(dirname(file.path(current_script))))
knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "RMD_FILE",
  "BASIC_OLS_FILE",
  "BLOCK_FE_FILE",
  "DL_I_THREE_COMP_FILE",
  "DL_V_SHOOT_OR_HOM_FILE",
  "DL_A_PARTONE_NON_SHOOT_OR_HOM_FILE",
  "DL_A_SHOOT_OR_HOM_FILE",
  "POISSON_FILE",
  "PAP_FILE",
  "OUTCOME_LABELS_FILE",
  "OUT_DIR"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
source('../R/table_functions.R')
table_config <- read_yaml('../hand/tables_config.yaml')
  
```


```{r}

source('R/alternate_spec_functions.R')

arrest_index <- read_csv(cl_args$BASIC_OLS_FILE, show_col_types = F) %>%
  filter(outcome == 'i_two_components') %>%
  select(-matches('tot')) %>%
  mutate(specification = 'OLS: Index pooling arrests into a single component')

block_fe <- read_csv(cl_args$BLOCK_FE_FILE, show_col_types = F) %>%
  select(-matches('tot')) %>%
  mutate(specification = 'OLS: No covariates except randomization blocks')

ols_pap <- read_csv(cl_args$PAP_FILE, show_col_types = F) %>%
  select(-matches('tot')) %>%
  mutate(specification = 'OLS: Alternative covariates + IPW')

poisson <- read_csv(cl_args$POISSON_FILE, show_col_types = F) %>%
  select(-ends_with('_itt')) %>%
  rename_with(~ gsub('_ame', '', .x), ends_with("_ame")) %>%
  mutate(specification = 'Poisson: Standard covariates')

double_lasso <- map_dfr(c(cl_args$DL_I_THREE_COMP_FILE, cl_args$DL_V_SHOOT_OR_HOM_FILE,
                          cl_args$DL_A_PARTONE_NON_SHOOT_OR_HOM_FILE, cl_args$DL_A_SHOOT_OR_HOM_FILE),
                        read_format_dl_result) %>%
  left_join(block_fe %>% select(outcome, subgroup, control_mean),
            by = c('outcome', 'subgroup')) %>% 
  relocate(specification, .after = control_mean)

ylabels <- read_csv(cl_args$OUTCOME_LABELS_FILE, show_col_types = F) %>%
  rename(outcome = value)

```

```{r}

results <- bind_rows(arrest_index, block_fe, double_lasso, ols_pap, poisson, .id = 'spec_order') %>%
  left_join(ylabels, by = "outcome") %>%
  arrange(spec_order, primary_component_order) %>%
  select(spec_order, primary_component_order, specification, label, control_mean, beta_itt, stderr_itt, pval_itt) %>%
  mutate(across(all_of(c('control_mean', 'beta_itt', 'stderr_itt')), ~ fmt(.x, decimals = 4))) %>%
  mutate(pval_itt = fmt(pval_itt, decimals = 3))

```

```{r}

table <- format_results(results)

table

timestamp <- get_timestamp()
writeLines(table, glue(cl_args$OUT_DIR, "/other_specifications_up_to_date.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/other_specifications_{timestamp}.tex"))

```

